package com.gph.saviorframework.oa.web;

import com.gph.saviorframework.OaConstants;
import com.gph.saviorframework.security.service.MenuService;
import com.gph.saviorframework.security.service.UserService;
import com.gph.saviorframework.uitl.DesUtils;
import com.gph.saviorframework.uitl.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by guopeihui on 2016/11/3.
 */
@Controller
public class IndexController {

    @Autowired
    private SecurityUtils securityUtils;

    @Autowired
    private MessageSourceAccessor messages;

    @Autowired
    private UserService userService;

    @Autowired
    MenuService menuService;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView indexPage() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("index");
        try {
        }
        catch (Exception ex){
            mv.setViewName("inerror");
            mv.addObject("error", ex.getMessage());
        }

        return mv;
    }

    //页面
    @RequestMapping(value = "/forgetpass", method = RequestMethod.GET)
    public ModelAndView myapply() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("forgetpass");
        return modelAndView;
    }

    //页面
    @RequestMapping(value = "/setpass", method = RequestMethod.GET)
    public ModelAndView setpass() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("setpass");
        return modelAndView;
    }

    /**
     * 密码重置接口
     *
     * @param uid
     * @param newPwd
     * @param confirmNewPwd
     * @return
     */
    @RequestMapping(value = "/resetpwd", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap restpwd(@RequestParam(value = "uid") String uid,
                            @RequestParam(value = "newPwd") String newPwd,
                            @RequestParam(value = "confirmNewPwd") String confirmNewPwd) {
        ModelMap modelMap = new ModelMap();
        try {
            if (uid == null || "".equals(uid)) {
                modelMap.addAttribute(OaConstants.DEFAULT_SUCCESS_KEY, Boolean.FALSE);
                modelMap.addAttribute("info", "链接非法, 请重新申请或联系系统管理员");
            }

            String descrit = DesUtils.decrypt(uid, OaConstants.DES_KEY);
            String hrefTime = descrit.split("#")[1];
            String userEmail = descrit.split("#")[0];


            SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");

            Date mailTime = format.parse(hrefTime);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(mailTime);
            calendar.add(Calendar.HOUR, 24);
            Date valTime = calendar.getTime();

            Date now = new Date();
            if (!now.before(valTime)) {
                modelMap.addAttribute(OaConstants.DEFAULT_SUCCESS_KEY, Boolean.FALSE);
                modelMap.addAttribute("info", "重置已过期, 请重新发送");

                return modelMap;
            }

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("USER_EMAIL", userEmail);
            Map<String, Object> userMap = userService.find("USER_EMAIL", "desc", map).get(0);
            String username = userMap.get("username").toString();

            if (newPwd.equals(confirmNewPwd)) {
                //修改密码
                userService.resetPassword(username, newPwd);
                modelMap.addAttribute(OaConstants.DEFAULT_SUCCESS_KEY, Boolean.TRUE);
                modelMap.addAttribute("info", "重置成功");
            } else {
                modelMap.addAttribute(OaConstants.DEFAULT_SUCCESS_KEY, Boolean.FALSE);
                modelMap.addAttribute("info", "重置失败");
            }

        } catch (ParseException e) {
            e.printStackTrace();
            modelMap.addAttribute(OaConstants.DEFAULT_SUCCESS_KEY, Boolean.FALSE);
            modelMap.addAttribute("info", "链接非法, 请重新申请或联系系统管理员");
        }
        return modelMap;
    }

}
